package com.mingqijia.gassafety.webserver.strategy.impl;

import cn.hutool.core.util.NumberUtil;
import com.gap.cache.redis.RedisCache;
import com.mingqijia.gassafety.db.entity.WarnStrategy;
import com.mingqijia.gassafety.db.entity.WarnStrategyExt;
import com.mingqijia.gassafety.db.mapper.ContactsMapper;
import com.mingqijia.gassafety.webserver.dto.PredateStrategyDTO;
import com.mingqijia.gassafety.webserver.dto.SendMessageDTO;
import com.mingqijia.gassafety.webserver.service.impl.CompanyServiceImpl;
import com.mingqijia.gassafety.webserver.service.strategy.WarnStrategyV2ServiceImpl;
import com.mingqijia.gassafety.webserver.strategy.PredicateGasWarnStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务到期当天 策略实现
 * </p>
 *
 * @author mby
 * @since 2024-01-16
 */
@Service
@Slf4j
public class PredicateGasNormalWarnStrategy implements PredicateGasWarnStrategy { // 实现策略

    @Autowired
    RedisCache redisCache;

    @Autowired
    ContactsMapper contactsMapper;

    @Autowired
    CompanyServiceImpl companyService;

    @Resource
    WarnStrategyV2ServiceImpl warnStrategyV2Service;

    /**
     * 信息发送判定，组装数据
     * @param dtuMap 报警策略
     * @param warnStrategyExtList 策略明细列表
     * @param cacheMap 缓存
     * @param sendDataList  组装的发送数据列表
     * @param predateStrategyDTO 信息发送判断，组装数据策略所需的数据
     */
    @Override
    public void predicateGasWarn(Map<String, Object> dtuMap, List<WarnStrategyExt> warnStrategyExtList, Map<String, Object> cacheMap,
                                 List<SendMessageDTO> sendDataList, PredateStrategyDTO predateStrategyDTO) {
        // 判断是不是正常
        if (!checkNormalReport(dtuMap.get("low_report") + "",dtuMap.get("high_report") + "", predateStrategyDTO.getValue())) {
            // 不是正常，返回
            return;
        }

        if (cacheMap != null && cacheMap.size() > 0) {
            // 执行删除缓存等操作
            log.info("warnStrategyV2-predicateGasNormalWarn-燃气泄露恢复正常,lowReport：{},highReport：{},value：{}", dtuMap.get("low_report") + "",dtuMap.get("high_report") + "", predateStrategyDTO.getValue());
            warnStrategyV2Service.deleteCache(predateStrategyDTO.getKey(), predateStrategyDTO.getImei(), cacheMap.get("identificationCode").toString(), predateStrategyDTO.getAlarmStrategykey());
        }
    }

    /**
     * 校验是否高报
     * @param lowReport  配置的低报值
     * @param highReport 配置的高报值
     * @param value 当前浓度值
     */
    public boolean checkNormalReport(String lowReport, String highReport, Integer value) {
        if (!StringUtils.isEmpty(lowReport) && NumberUtil.isNumber(lowReport)) {
            if (value >= Integer.parseInt(lowReport)) {
                // 大于等于低报值
                return false;
            }
        }
        if (!StringUtils.isEmpty(highReport) && NumberUtil.isNumber(highReport)) {
            if (value >= Integer.parseInt(highReport)) {
                // 大于等于高报值
                return false;
            }
        }
        return true;
    }

}
